package com.coin900.coin.cmd.sop;

import com.google.gson.Gson;
import com.coin900.coin.cmd.ACmd;
import com.coin900.coin.cmd.CmdSign;
import com.coin900.coin.code.CmdCode;
import com.coin900.coin.persist.fu.FuUserVO;
import com.coin900.coin.persist.sop.SopMenuVO;
import com.coin900.coin.service.sop.ISopMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 新增或修改菜单
 *
 * @author shui
 * @create 2018-01-09
 **/
@Controller
public class SopMenuPutCmd extends ACmd {
    @Override
    public CmdSign execute(HttpServletRequest request, CmdSign sign) throws Exception {
        CmdSign respSign = new CmdSign(sign);
        FuUserVO user = super.getUser(sign);
        String source = sign.getSource();
        SopMenuVO sopMenuVO = new Gson().fromJson(source, SopMenuVO.class);
        Long id = sopMenuVO.getId();
        Map<String, Object> params = new HashMap<>();
        if (id == null) {
            // 新增
            sopMenuVO.setCreatedInfor(user);
            this.menuService.insertSopMenu(sopMenuVO);
        } else {
            // 修改
            params.put("id", id);
            params.put("dataStatus", 1);
            List<SopMenuVO> sopMenuVOS = this.menuService.loadSopMenu(params);
            if (!sopMenuVOS.isEmpty()) {
                sopMenuVO.setModifiedInfor(user);
                this.menuService.updateSopMenu(sopMenuVO);
            }
        }
        return respSign;
    }

    @Autowired
    private ISopMenuService menuService;

    @Override
    public String getCmdCode() {
        return CmdCode.SOP_MENU_PUT;
    }
}
